<?php

	class Smash{
		
		var $specs = array();
		
		var $terminal_session;
		
		var $command;
		
		function __construct(){
			if(!ob_get_level()) ob_start();
		}
		
		function qline(){
			return usr()->name.'@'.sm_getcwdom().' ['.sm_cwd()->basename().']# ';
		}
		
		function run(){
			$this->execute($_POST);	
		}
		
		function execute($ops){
			if(is_array($ops)){
				if(!isset($ops['com'])) trigger_error("-smash: no command to execute", E_USER_ERROR);
				$this->command = $ops['com'];
				if(isset($ops['cwd']) && !sm_chdir($ops['cwd'])) trigger_error("-smash: could not cd to {$ops['cwd']}", E_USER_ERROR);
				if(isset($ops['tsn'])) $this->terminal_session = $ops['tsn'];
			}else $this->command = $ops;
			$spec = new comSpec($this->command);
			$prog = $spec->program;
			if(!$program = $this->open($prog, $spec)) trigger_error("-smash: $prog: command not found", E_USER_ERROR);
			$program->run();
			smout()->write();
		}
		
		function open($prog, $spec){
			if(!is_file($php = SMERNEL_PATH."/bin/$prog.php")){
				if(!is_file($php = sm_realpath("desk:/Scripts/bin/$prog.php"))) return false;
			}
			require_once $php;
			$class = "Command_$prog";
			return new $class($spec);
		}
		
		function close(){
			foreach($this->specs as $spec) $spec->close();	
		}
		
		function __destruct(){
			$this->close();	
		}
			
	}


